package com.yiban.demo.ShuoShuo.Controller;

import com.yiban.demo.Global.Entity.ResponseEntity;
import com.yiban.demo.LoginUser.Controller.LoginUserController;
import com.yiban.demo.LoginUser.Entity.LoginUserPO;
import com.yiban.demo.ShuoShuo.Entity.ShuoShuo;
import com.yiban.demo.ShuoShuo.Entity.ShuoShuoComment;
import com.yiban.demo.ShuoShuo.Entity.TotalShuoShuo;
import com.yiban.demo.ShuoShuo.Service.ShuoShuoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/ShuoShuo")
public class ShuoShuoController {
    @Autowired
    private ShuoShuoService shuoShuoService;

    @PostMapping("/shuoshuo")
    public ResponseEntity<Boolean> publishShuoShuo(@Validated @RequestBody ShuoShuo shuoShuo){
        return ResponseEntity.Success(shuoShuoService.publishShuoShuo(shuoShuo));
    }
    @PostMapping("/star/{shuoshuoId}")
    public ResponseEntity<Void> starShuoShuo(@PathVariable Integer shuoshuoId, @SessionAttribute(value = LoginUserController.USER_SESSION_KEY)LoginUserPO loginUserPO){
        shuoShuoService.starShuoShuo(shuoshuoId,loginUserPO.getUserId());
        return ResponseEntity.Success(null);
    }
    @DeleteMapping("/star/{shuoshuoId}")
    public ResponseEntity<Void> cancelStar(@PathVariable Integer shuoshuoId, @SessionAttribute(value = LoginUserController.USER_SESSION_KEY)LoginUserPO loginUserPO){
        shuoShuoService.cancelStar(shuoshuoId, loginUserPO.getUserId());
        return ResponseEntity.Success(null);
    }
    @GetMapping("/shuoshuo/{isTotal}/{current}/{size}")
    public ResponseEntity<List<?>> getTotalShuoshuo(@PathVariable Integer size, @PathVariable Integer current, @PathVariable int isTotal){
        return isTotal == 1 ?ResponseEntity.Success(shuoShuoService.getTotalShuoShuo(current, size)) : ResponseEntity.Success(shuoShuoService.getShuoShuo(current, size));
    }

    @PostMapping("/shuoshuoComment")
    public ResponseEntity<Boolean> publishComment(@RequestBody @Validated ShuoShuoComment shuoShuoComment,@SessionAttribute(LoginUserController.USER_SESSION_KEY)LoginUserPO loginUserPO){
        return ResponseEntity.Success(shuoShuoService.publishShuoShuoComment(shuoShuoComment, loginUserPO.getUserId()));
    }
    @GetMapping("/shuoshuo/timeline/{current}/{size}/{start}/{end}")
    public ResponseEntity<List<ShuoShuo>> timeLine(@PathVariable Long current, @PathVariable Long size, @PathVariable Long start, @PathVariable Long end){
        return ResponseEntity.Success(shuoShuoService.getByTime(start,end,current,size));
    }
    @GetMapping("/shuoshuo/rank/{current}/{size}")
    public ResponseEntity<List<ShuoShuo>> getRank(@PathVariable Long current, @PathVariable Long size){
        return ResponseEntity.Success(shuoShuoService.getByStarCount(current,size));
    }
}
